import { getBannerList, getSecondKillGoods, getGoodsList } from '@/api/home'
import nanoid from '@/lib/nanoid'
import { ref, type Ref, computed } from 'vue'
type BannerItem = {
  alt: string
  bannerid: string
  flag: boolean
  img: string
  link: string
}

type gridItem = {
  id: string
  text: string
  url: string
}

type SecSkillItem = {
  proid: string,
  img1: string,
  originprice: string,
  [propName: string]: any
}

export type GoodsItem = {
  proid: string,
  img1: string,
  proname: string,
  originprice: string,
  [propName: string]: any
}

export async function useBanner(): Promise<BannerItem[]> {
  const res = await getBannerList()
  const bannerList: BannerItem[] = res.data
  return bannerList
}


export function useGridList() {
  const gridList: gridItem[] = [
    {
      id: nanoid(),
      text: '京东超市',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/175540/24/19329/6842/60ec0b0aEf35f7384/ec560dbf9b82b90b.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '数码电器',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/178015/31/13828/6862/60ec0c04Ee2fd63ac/ccf74d805a059a44.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '京东新百货',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/54043/33/19389/4660/62b049dbE3b9aef75/2fcd31afd5d702e4.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '京东生鲜',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/177902/16/13776/5658/60ec0e71E801087f2/a0d5a68bf1461e6d.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '京东到家',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/196472/7/12807/7127/60ec0ea3Efe11835b/37c65625d94cae75.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '京东超市',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/175540/24/19329/6842/60ec0b0aEf35f7384/ec560dbf9b82b90b.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '数码电器',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/178015/31/13828/6862/60ec0c04Ee2fd63ac/ccf74d805a059a44.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '京东新百货',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/54043/33/19389/4660/62b049dbE3b9aef75/2fcd31afd5d702e4.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '京东生鲜',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/177902/16/13776/5658/60ec0e71E801087f2/a0d5a68bf1461e6d.png!q70.jpg',
    },
    {
      id: nanoid(),
      text: '京东到家',
      url:
        'https://m15.360buyimg.com/mobilecms/jfs/t1/196472/7/12807/7127/60ec0ea3Efe11835b/37c65625d94cae75.png!q70.jpg',
    },
  ]
  const handleGriditemClick = (id: string) => {
    console.log(id)
  }
  return { gridList, handleGriditemClick }
}

export async function useSecSkill() {
  function getRemainTime() {
    const _hour = ref(0)
    const _time = ref(0)

    const date = new Date()
    let hour = date.getHours()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    if (hour % 2 === 0) {
      hour += 2
    } else {
      hour += 1
    }
    // 用于显示几点场
    _hour.value = hour - 2
    if (hour === 24) {
      hour = 0
      day += 1
      // 你要是不知道13578十腊，我看你这个ios兼容怎么写
      if (
        (day === 31 && [1, 3, 5, 7, 8, 10, 11].indexOf(month) === -1) ||
        (day === 32 && [1, 3, 5, 7, 8, 10, 11].indexOf(month) !== -1)
      ) {
        day = 1
        month += 1
        if (month === 13) {
          month = 1
          year += 1
        }
      }
    }
    // 计算剩余时间的数字
    _time.value =
      new Date(`${year}/${month}/${day} ${hour}:00:00`).getTime() -
      date.getTime()
    return {
      _hour,
      _time
    }
  }


  const res = await getSecondKillGoods()
  const miaoshaList: SecSkillItem[] = res.data
  const { _hour, _time } = getRemainTime()

  return {
    miaoshaList,
    hour: _hour,
    time: _time
  }
}

export async function useGoodsList() {
  let currentCount = 1
  // 上拉加载更多使用
  const loading = ref(false)
  // 下拉刷新使用
  const isLoading = ref(false)
  // 上拉无更多数据使用
  const finished = ref(false)
  const goodsList = <Ref<GoodsItem[]>>ref([])
  function getGoodsData() {
    loading.value = true
    return getGoodsList(currentCount++)
      .then((res) => {
        loading.value = false
        if (res.data.length === 0) {
          finished.value = true
          return false
        }
        goodsList.value = goodsList.value.concat(res.data)
      })
      .catch((err) => {
        console.error(err)
      })
  }
  // 下拉刷新功能
  function refresh() {
    currentCount = 1
    goodsList.value = []
    getGoodsData().then(() => {
      isLoading.value = false
    })
  }
  const formatTime = computed(() => (num: number) => {
    return num < 10 ? '0' + num : num
  })
  return {
    goodsList,
    loading,
    isLoading,
    finished,
    getGoodsData,
    formatTime,
    refresh
  }
}